
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>php流程控制 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="08_php数组.html" />
    
    
    <link rel="prev" href="06_php运算符.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01_php介绍.html">
            
                <a href="01_php介绍.html">
            
                    
                    php基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="01_php介绍.html">
            
                <a href="01_php介绍.html">
            
                    
                    php介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="02_PHP基础语法.html">
            
                <a href="02_PHP基础语法.html">
            
                    
                    php基础语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="03_php变量.html">
            
                <a href="03_php变量.html">
            
                    
                    php变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="04_php数据类型.html">
            
                <a href="04_php数据类型.html">
            
                    
                    php数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="05_php常量.html">
            
                <a href="05_php常量.html">
            
                    
                    php常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="06_php运算符.html">
            
                <a href="06_php运算符.html">
            
                    
                    php运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.7" data-path="07_php流程控制.html">
            
                <a href="07_php流程控制.html">
            
                    
                    php流程控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="08_php数组.html">
            
                <a href="08_php数组.html">
            
                    
                    php数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="09_php函数.html">
            
                <a href="09_php函数.html">
            
                    
                    php函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="10_php面向对象.html">
            
                <a href="10_php面向对象.html">
            
                    
                    php面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="11_php命名空间.html">
            
                <a href="11_php命名空间.html">
            
                    
                    php命名空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="12_php日期和时间.html">
            
                <a href="12_php日期和时间.html">
            
                    
                    php日期时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.13" data-path="13_php表单.html">
            
                <a href="13_php表单.html">
            
                    
                    php表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.14" data-path="14_php包含文件.html">
            
                <a href="14_php包含文件.html">
            
                    
                    php包含文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.15" data-path="15_php常用函数.html">
            
                <a href="15_php常用函数.html">
            
                    
                    php常用函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.16" data-path="../php高级/16_php常用变量函数.md">
            
                <span>
            
                    
                    php常用变量函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../php高级/01_php错误与异常.html">
            
                <a href="../php高级/01_php错误与异常.html">
            
                    
                    php错误与异常
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../php高级/02_PhpStorm本地断点调试.html">
            
                <a href="../php高级/02_PhpStorm本地断点调试.html">
            
                    
                    PhpStorm本地断点调试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../php高级/03_php文件操作.html">
            
                <a href="../php高级/03_php文件操作.html">
            
                    
                    php文件操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../php高级/04_cookie机制.html">
            
                <a href="../php高级/04_cookie机制.html">
            
                    
                    cookie机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../php高级/05_session.html">
            
                <a href="../php高级/05_session.html">
            
                    
                    session
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../php高级/06_php画图.html">
            
                <a href="../php高级/06_php画图.html">
            
                    
                    php画图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../php高级/07_php加密机制.html">
            
                <a href="../php高级/07_php加密机制.html">
            
                    
                    php加密机制
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >php流程控制</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6D41;&#x7A0B;&#x63A7;&#x5236;">&#x6D41;&#x7A0B;&#x63A7;&#x5236;</h1>
<h2 id="&#x9009;&#x62E9;&#x7ED3;&#x6784;">&#x9009;&#x62E9;&#x7ED3;&#x6784;</h2>
<blockquote>
<p>&#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x7528;&#x4E8E;&#x6839;&#x636E;&#x4E0D;&#x540C;&#x6761;&#x4EF6;&#x6267;&#x884C;&#x4E0D;&#x540C;&#x52A8;&#x4F5C;&#x3002;</p>
</blockquote>
<h3 id="ifelse-&#x8BED;&#x53E5;">If...Else &#x8BED;&#x53E5;</h3>
<p>&#x5728; PHP &#x4E2D;&#xFF0C;&#x63D0;&#x4F9B;&#x4E86;&#x4E0B;&#x5217;&#x6761;&#x4EF6;&#x8BED;&#x53E5;&#xFF1A;</p>
<ul>
<li>if &#x8BED;&#x53E5; - &#x5728;&#x6761;&#x4EF6;&#x6210;&#x7ACB;&#x65F6;&#x6267;&#x884C;&#x4EE3;&#x7801;</li>
<li>if...else &#x8BED;&#x53E5; - &#x5728;&#x6761;&#x4EF6;&#x6210;&#x7ACB;&#x65F6;&#x6267;&#x884C;&#x4E00;&#x5757;&#x4EE3;&#x7801;&#xFF0C;&#x6761;&#x4EF6;&#x4E0D;&#x6210;&#x7ACB;&#x65F6;&#x6267;&#x884C;&#x53E6;&#x4E00;&#x5757;&#x4EE3;&#x7801;</li>
<li>if...else if....else &#x8BED;&#x53E5; - &#x5728;&#x82E5;&#x5E72;&#x6761;&#x4EF6;&#x4E4B;&#x4E00;&#x6210;&#x7ACB;&#x65F6;&#x6267;&#x884C;&#x4E00;&#x4E2A;&#x4EE3;&#x7801;&#x5757;</li>
<li>switch &#x8BED;&#x53E5; - &#x5728;&#x82E5;&#x5E72;&#x6761;&#x4EF6;&#x4E4B;&#x4E00;&#x6210;&#x7ACB;&#x65F6;&#x6267;&#x884C;&#x4E00;&#x4E2A;&#x4EE3;&#x7801;&#x5757;</li>
</ul>
<h4 id="if-&#x8BED;&#x53E5;">if &#x8BED;&#x53E5;</h4>
<p>if &#x8BED;&#x53E5;&#x7528;&#x4E8E;&#x4EC5;&#x5F53;&#x6307;&#x5B9A;&#x6761;&#x4EF6;&#x6210;&#x7ACB;&#x65F6;&#x6267;&#x884C;&#x4EE3;&#x7801;&#x3002; &#x8BED;&#x6CD5;</p>
<pre><code class="lang-php"><span class="hljs-keyword">if</span> (&#x6761;&#x4EF6;)
{
    &#x6761;&#x4EF6;&#x6210;&#x7ACB;&#x65F6;&#x8981;&#x6267;&#x884C;&#x7684;&#x4EE3;&#x7801;;
}
</code></pre>
<h4 id="ifelse-&#x8BED;&#x53E5;">if...else &#x8BED;&#x53E5;</h4>
<p>&#x5728;&#x6761;&#x4EF6;&#x6210;&#x7ACB;&#x65F6;&#x6267;&#x884C;&#x4E00;&#x5757;&#x4EE3;&#x7801;&#xFF0C;&#x6761;&#x4EF6;&#x4E0D;&#x6210;&#x7ACB;&#x65F6;&#x6267;&#x884C;&#x53E6;&#x4E00;&#x5757;&#x4EE3;&#x7801;&#xFF0C;&#x8BF7;&#x4F7F;&#x7528; if....else &#x8BED;&#x53E5;&#x3002; &#x8BED;&#x6CD5;</p>
<pre><code class="lang-php"><span class="hljs-keyword">if</span> (&#x6761;&#x4EF6;)
{
&#x6761;&#x4EF6;&#x6210;&#x7ACB;&#x65F6;&#x6267;&#x884C;&#x7684;&#x4EE3;&#x7801;;
}
<span class="hljs-keyword">else</span>
{
&#x6761;&#x4EF6;&#x4E0D;&#x6210;&#x7ACB;&#x65F6;&#x6267;&#x884C;&#x7684;&#x4EE3;&#x7801;;
}
</code></pre>
<h4 id="ifelse-ifelse-&#x8BED;&#x53E5;">if...else if....else &#x8BED;&#x53E5;</h4>
<p>&#x5728;&#x82E5;&#x5E72;&#x6761;&#x4EF6;&#x4E4B;&#x4E00;&#x6210;&#x7ACB;&#x65F6;&#x6267;&#x884C;&#x4E00;&#x4E2A;&#x4EE3;&#x7801;&#x5757;&#xFF0C;&#x8BF7;&#x4F7F;&#x7528; if....else if...else &#x8BED;&#x53E5;&#x3002;. &#x8BED;&#x6CD5;</p>
<pre><code class="lang-php"><span class="hljs-keyword">if</span> (&#x6761;&#x4EF6;)
{
<span class="hljs-keyword">if</span> &#x6761;&#x4EF6;&#x6210;&#x7ACB;&#x65F6;&#x6267;&#x884C;&#x7684;&#x4EE3;&#x7801;;
}
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (&#x6761;&#x4EF6;)
{
<span class="hljs-keyword">elseif</span> &#x6761;&#x4EF6;&#x6210;&#x7ACB;&#x65F6;&#x6267;&#x884C;&#x7684;&#x4EE3;&#x7801;;
}
<span class="hljs-keyword">else</span>
{
&#x6761;&#x4EF6;&#x4E0D;&#x6210;&#x7ACB;&#x65F6;&#x6267;&#x884C;&#x7684;&#x4EE3;&#x7801;;
}
</code></pre>
<h3 id="switch-&#x8BED;&#x53E5;">Switch &#x8BED;&#x53E5;</h3>
<p>&#x5982;&#x679C;&#x60A8;&#x5E0C;&#x671B;&#x6709;&#x9009;&#x62E9;&#x5730;&#x6267;&#x884C;&#x82E5;&#x5E72;&#x4EE3;&#x7801;&#x5757;&#x4E4B;&#x4E00;&#xFF0C;&#x8BF7;&#x4F7F;&#x7528; switch &#x8BED;&#x53E5;&#x3002;</p>
<pre><code class="lang-php">&#x8BED;&#x6CD5;
<span class="hljs-keyword">switch</span> (n)
{
<span class="hljs-keyword">case</span> label1:
    &#x5982;&#x679C; n=label1&#xFF0C;&#x6B64;&#x5904;&#x4EE3;&#x7801;&#x5C06;&#x6267;&#x884C;;
    <span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> label2:
    &#x5982;&#x679C; n=label2&#xFF0C;&#x6B64;&#x5904;&#x4EE3;&#x7801;&#x5C06;&#x6267;&#x884C;;
    <span class="hljs-keyword">break</span>;
<span class="hljs-keyword">default</span>:
    &#x5982;&#x679C; n &#x65E2;&#x4E0D;&#x7B49;&#x4E8E; label1 &#x4E5F;&#x4E0D;&#x7B49;&#x4E8E; label2&#xFF0C;&#x6B64;&#x5904;&#x4EE3;&#x7801;&#x5C06;&#x6267;&#x884C;;
}
</code></pre>
<h2 id="&#x5FAA;&#x73AF;">&#x5FAA;&#x73AF;</h2>
<h3 id="for-&#x5FAA;&#x73AF;">For &#x5FAA;&#x73AF;</h3>
<blockquote>
<p>&#x5FAA;&#x73AF;&#x6267;&#x884C;&#x4EE3;&#x7801;&#x5757;&#x6307;&#x5B9A;&#x7684;&#x6B21;&#x6570;&#xFF0C;&#x6216;&#x8005;&#x5F53;&#x6307;&#x5B9A;&#x7684;&#x6761;&#x4EF6;&#x4E3A;&#x771F;&#x65F6;&#x5FAA;&#x73AF;&#x6267;&#x884C;&#x4EE3;&#x7801;&#x5757;&#x3002;</p>
</blockquote>
<p>for&#x5FAA;&#x73AF;&#x7528;&#x4E8E;&#x9884;&#x5148;&#x77E5;&#x9053;&#x811A;&#x672C;&#x8981;&#x8FD0;&#x884C;&#x7684;&#x6B21;&#x6570;&#x3002; &#x8BED;&#x6CD5;</p>
<pre><code class="lang-php"><span class="hljs-keyword">for</span> (&#x521D;&#x59CB;&#x503C;; &#x6761;&#x4EF6;; &#x589E;&#x91CF;)
{
&#x8981;&#x6267;&#x884C;&#x7684;&#x4EE3;&#x7801;;
}
</code></pre>
<p>&#x793A;&#x4F8B;</p>
<pre><code class="lang-php">&lt;html&gt;
&lt;body&gt;

<span class="hljs-meta">&lt;?php</span>
<span class="hljs-keyword">for</span> ($i=<span class="hljs-number">1</span>; $i&lt;=<span class="hljs-number">5</span>; $i++)
{
<span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;The number is &quot;</span> . $i . <span class="hljs-string">&quot;&lt;br&gt;&quot;</span>;
}
<span class="hljs-meta">?&gt;</span>

&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<h3 id="foreach-&#x5FAA;&#x73AF;">foreach &#x5FAA;&#x73AF;</h3>
<p>foreach &#x5FAA;&#x73AF;&#x7528;&#x4E8E;&#x904D;&#x5386;&#x6570;&#x7EC4;&#x3002;</p>
<pre><code class="lang-php"><span class="hljs-keyword">foreach</span> ($array <span class="hljs-keyword">as</span> $value)
{
&#x8981;&#x6267;&#x884C;&#x4EE3;&#x7801;;
}
<span class="hljs-keyword">foreach</span> ($array <span class="hljs-keyword">as</span> $key=&gt;$value)
{
&#x8981;&#x6267;&#x884C;&#x4EE3;&#x7801;;
}
</code></pre>
<h3 id="while-&#x5FAA;&#x73AF;">While &#x5FAA;&#x73AF;</h3>
<p>while &#x5FAA;&#x73AF;&#x5C06;&#x91CD;&#x590D;&#x6267;&#x884C;&#x4EE3;&#x7801;&#x5757;&#xFF0C;&#x76F4;&#x5230;&#x6307;&#x5B9A;&#x7684;&#x6761;&#x4EF6;&#x4E0D;&#x6210;&#x7ACB;&#x3002;</p>
<pre><code class="lang-php"><span class="hljs-keyword">while</span> (&#x6761;&#x4EF6;)
{
&#x8981;&#x6267;&#x884C;&#x7684;&#x4EE3;&#x7801;;
}
</code></pre>
<h3 id="dowhile-&#x8BED;&#x53E5;">do...while &#x8BED;&#x53E5;</h3>
<p>do...while &#x8BED;&#x53E5;&#x4F1A;&#x81F3;&#x5C11;&#x6267;&#x884C;&#x4E00;&#x6B21;&#x4EE3;&#x7801;&#xFF0C;&#x7136;&#x540E;&#x68C0;&#x67E5;&#x6761;&#x4EF6;&#xFF0C;&#x53EA;&#x8981;&#x6761;&#x4EF6;&#x6210;&#x7ACB;&#xFF0C;&#x5C31;&#x4F1A;&#x91CD;&#x590D;&#x8FDB;&#x884C;&#x5FAA;&#x73AF;&#x3002; &#x8BED;&#x6CD5;</p>
<pre><code class="lang-php"><span class="hljs-keyword">do</span>
{
&#x8981;&#x6267;&#x884C;&#x7684;&#x4EE3;&#x7801;;
}
<span class="hljs-keyword">while</span> (&#x6761;&#x4EF6;);
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="06_php运算符.html" class="navigation navigation-prev " aria-label="Previous page: php运算符">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="08_php数组.html" class="navigation navigation-next " aria-label="Next page: php数组">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"php流程控制","level":"1.2.7","depth":2,"next":{"title":"php数组","level":"1.2.8","depth":2,"path":"php/php基础/08_php数组.md","ref":"./php/php基础/08_php数组.md","articles":[]},"previous":{"title":"php运算符","level":"1.2.6","depth":2,"path":"php/php基础/06_php运算符.md","ref":"./php/php基础/06_php运算符.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"php/php基础/07_php流程控制.md","mtime":"2020-04-23T09:59:52.387Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-04-23T11:06:38.883Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

